# -*- coding: utf-8 -*-
import scrapy
import re
from bookinfo.items import BookinfoItem
class bookinfoSpider(scrapy.Spider):
    name = "bookinfo"
    start_urls = [ "https://book.douban.com/top250" ]
    def parse(self,response):
        yield scrapy.Request(response.url,callback=self.parse_page)
        for page_url in response.xpath('//div[@class="paginator"]/a/@href').extract():
            yield scrapy.Request(page_url,callback=self.parse_page)
    def parse_page(self,response):
        for item in response.xpath('//div[@class="article"]/div[1]/table/tr[1]'):
            bookinfo=BookinfoItem()  #在for循环内实例化在item中定义的各字段，抓取每个item使用一个新的bookinfo空间，相互不影响
            bookinfo['name']=item.xpath("td[2]/div[1]/a/text()").extract()[0].strip()
            bookinfo['price']=item.xpath("td[2]/p/text()").extract()[0].strip().split("/")[-1]
            bookinfo['editor_date']=item.xpath("td[2]/p/text()").extract()[0].strip().split("/")[-2]
            bookinfo['publisher']=item.xpath("td[2]/p/text()").extract()[0].strip().split("/")[-3]
            bookinfo['author']=item.xpath("td[2]/p/text()").extract()[0].strip().split("/")[-4]
            bookinfo['rating']=item.xpath("td[2]/div[2]/span[2]/text()").extract()[0]
            bookinfo['image_urls']=item.xpath("td[1]/a/img/@src").extract_first()
            yield bookinfo
